What is mobile-detect?
The 'mobile-detect' npm package is a lightweight JavaScript library used to detect mobile devices, operating systems, and user agents. It helps developers tailor their web applications to different devices by providing detailed information about the user's device.
What are mobile-detect's main functionalities?
Detect Mobile Device
This feature allows you to detect if the user is on a mobile device and identify the type of device. The code sample demonstrates how to use the 'mobile' method to get the device type.
const MobileDetect = require('mobile-detect');
const md = new MobileDetect(window.navigator.userAgent);
console.log(md.mobile()); // 'iPhone' for iPhone, 'Android' for Android devices, null for desktop
Detect Operating System
This feature allows you to detect the operating system of the user's device. The code sample shows how to use the 'os' method to get the operating system.
const MobileDetect = require('mobile-detect');
const md = new MobileDetect(window.navigator.userAgent);
console.log(md.os()); // 'iOS' for iPhone, 'AndroidOS' for Android devices, null for desktop
Detect User Agent
This feature allows you to detect the user agent of the browser. The code sample demonstrates how to use the 'userAgent' method to get the browser's user agent.
const MobileDetect = require('mobile-detect');
const md = new MobileDetect(window.navigator.userAgent);
console.log(md.userAgent()); // 'Safari' for Safari browser, 'Chrome' for Chrome browser, etc.
Detect Phone Model
This feature allows you to detect the specific phone model. The code sample shows how to use the 'phone' method to get the phone model.
const MobileDetect = require('mobile-detect');
const md = new MobileDetect(window.navigator.userAgent);
console.log(md.phone()); // 'iPhone' for iPhone, 'Nexus' for Nexus devices, etc.
Other packages similar to mobile-detect
ua-parser-js
The 'ua-parser-js' package is a JavaScript library for parsing user agent strings. It provides detailed information about the browser, engine, OS, CPU, and device. Compared to 'mobile-detect', 'ua-parser-js' offers more comprehensive parsing capabilities and supports a wider range of devices and browsers.
detect-browser
The 'detect-browser' package is a lightweight library for detecting the browser and its version. It is simpler and more focused on browser detection compared to 'mobile-detect', which provides broader device and OS detection capabilities.
platform
The 'platform' package is a library for detecting the platform (operating system, browser, and engine) of the user's device. It provides detailed information similar to 'mobile-detect' but focuses more on the platform rather than the specific device type.
mobile-detect.js
A loose port of Mobile-Detect to JavaScript.
This script will detect the device by comparing patterns against a given User-Agent string.
You can find out information about the device rendering your web page:
- mobile or not
- if mobile, whether phone or tablet
- operating system
- Mobile Grade (A, B, C) REMARK: this is completely outdated: all current devices will return an 'A', so it's useless nowadays
- specific versions (e.g. WebKit)
Current master
branch is using detection logic from Mobile-Detect@2.8.37
Live Demo
Demo/check (sorry about the missing styling) can be found here.
Warning
TL;DR: you should not use this library in your HTML page and it's less reliable when used server-side (Node.js)
As mentioned later, "User-Agent" based detection is not a reliable solution in most cases, because:
- The rules (regular expressions) are constantly out-dated and incomplete
- You have to update the detection code continuously
- There are other ways to detect how your web application should behave:
- Maybe there are some libraries out there (which are probably not free) doing a more reliable job
If you still want to (or have to) use this library, you should always encapsulate it with your own code,
because chances a very high that you have to tweak the behaviour a bit or are not satisfied with the
result of mobile-detect.js. Don't spread usage of MobileDetect all over your own code! As you can see
in the issues, there are some "bugs", feature-requests, pull-requests where people are not so happy
how MobileDetect works. But I cannot change its behaviour from version to version, even if this was
reasonable from new users' point of view. I hope you show understanding.
At least there is a way to monkey-patch the library (see "Extending" below).
Usage
Browser
<script src="mobile-detect.js"></script>
<script>
var md = new MobileDetect(window.navigator.userAgent);
</script>
Node.js / Express
var MobileDetect = require('mobile-detect'),
md = new MobileDetect(req.headers['user-agent']);
General
var md = new MobileDetect(
'Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i' +
' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)' +
' Version/4.0 Mobile Safari/534.30');
console.log( md.mobile() );
console.log( md.phone() );
console.log( md.tablet() );
console.log( md.userAgent() );
console.log( md.os() );
console.log( md.is('iPhone') );
console.log( md.is('bot') );
console.log( md.version('Webkit') );
console.log( md.versionStr('Build') );
console.log( md.match('playstation|xbox') );
More Info ...
There is some documentation generated by JSDoc:
https://hgoebl.github.io/mobile-detect.js/doc/MobileDetect.html
Side Effects
Script creates the global property MobileDetect
.
Modernizr Extension
When using Modernizr, you can include mobile-detect-modernizr.js
.
It will add the CSS classes mobile
, phone
, tablet
and mobilegradea
if applicable.
You can easily extend it, e.g. android
, iphone
, etc.
Size (bytes)
- development: 70168
- minified: 39585
- minified + gzipped: 16556 (
cat mobile-detect.min.js | gzip -9f | wc -c
)
Installation
Bower (which you should not use for new projects)
$ bower install hgoebl/mobile-detect.js --save
Node.js / npm
$ npm install mobile-detect --save

<script src="https://cdn.jsdelivr.net/npm/mobile-detect@1.4.5/mobile-detect.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.5/mobile-detect.min.js"></script>
Extending/Modifying Behaviour
Though it is not recommended to rely on internal methods or structures of MobileDetect, you can alter
the behaviour by replacing particular internal methods with your own implementations.
If you feel like this is the only possibility, then go ahead and have a look at the source code and
examples in tests/spec/MobileDetectSpec.js (search for "Extensibility").
Alternatives / Infos
Often device detection is the first solution in your mind. Please consider looking for other solutions
like media queries and feature detection (e.g. w/ Modernizr). Maybe there are better (simpler, smaller,
faster) device detection libraries, so here you have a list (order has no meaning apart from first element):
- REMARK: this list might be outdated - please tell me about alternatives!
- Modernizr
In most cases the better solution: don't use knowledge about device and version, but detect features
(touch, canvas, ...)
- Mozilla Hacks - User-Agent detection, history and checklist
- Mobile-Detect
A lightweight PHP class for detecting mobile devices (including tablets).
This is the "source" of this JavaScript project and if you use PHP on your server you should use it!
- Detect Mobile Browsers Open source mobile phone detection in many languages
and for Webservers (Apache, nginx, IIS). mobile-detect.js uses the code of this library as a fallback in case
of incomplete detection regular expressions.
- sebarmeli / JS-Redirection-Mobile-Site
JS to handle the redirection to the mobile version of your site
- dmolsen/Detector
Detector is a simple, PHP- and JavaScript-based browser- and
feature-detection library that can adapt to new devices & browsers
on its own without the need to pull from a central database of browser information.
- matthewhudson/device.js
Conditional CSS and/or JavaScript based on device operating system, orientation and type
- brendanlim/mobile-fu
Automatically detect mobile requests from mobile devices in your Rails application.
- FormFactor
FormFactor helps you customize your web app for different form factors, e.g. when you make
"the mobile version", "the TV version", etc.
- UAParser.js
Lightweight JavaScript-based User-Agent String Parser
- MobileESP - Easily detect mobile web site visitors
- WURFL
- Handset and Tablet Detection
- Search on microjs.com
Mobile-Usage Statistics
If you have to provide statistics about how many and which mobile devices are hitting your web-site, you can
generate statistics (data and views) with hgoebl/mobile-usage.
There are many hooks to customize statistical calculation to your needs.
License
MIT-License (see LICENSE file).
Contributing
Your contribution is welcome.
If you want new devices to be supported, please contribute to
Mobile-Detect instead.
To run generate-script it is necessary to have Mobile-Detect
as a sibling directory to mobile-detect.js/.
(I tried to use git subtree
but had some problems on Mac OS X - probably my fault...)
- fork or clone serbanghita/Mobile-Detect
- fork hgoebl/mobile-detect.js
- run
npm install
- create branch
- make changes and run
grunt
(needs PHP >= 5.4 in your path) - run browser test (tests/SpecRunner.html)
- commit, push to your branch
- create pull request
Testing
Browser
Open tests/SpecRunner.html
in different browsers.
Node.js
$ npm test
$ # or
$ grunt jasmine_node
Donations
If you want, you can donate to Mobile-Detect.
TODO
- Extend RegEx patterns so that test passes
- support ES6 modules